﻿using QuanLySieuThi.Controllers;
using QuanLySieuThi.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLySieuThi.Views
{
    public partial class frmNhanVien : Form
    {
        public static string matkhau = "123";
        public frmNhanVien()
        {
            InitializeComponent();
        }

        private void Click_ThemNhanVien(object sender, EventArgs e)
        {
            NhanVien nv = GetNhanVienFromView();
            frmMatKhau f = new frmMatKhau();
            var r = f.ShowDialog();
            if (r == System.Windows.Forms.DialogResult.OK)
            {
                nv.MatKhau = matkhau;
                QuanLyNhanVien.UniqueObject.Add(nv);
                dataGridView1.DataSource = QuanLyNhanVien.UniqueObject.GetDataTable;
                MessageBox.Show("OK");            
            }
        }

        private void click_CapNhat(object sender, EventArgs e)
        {
            QuanLyNhanVien.UniqueObject.Update(GetNhanVienFromView());
            dataGridView1.DataSource = QuanLyNhanVien.UniqueObject.GetDataTable;
            MessageBox.Show("OK");
        }

        private void click_Xoa(object sender, EventArgs e)
        {
            
            var r = MessageBox.Show("Bạn có muốn xóa nhân viên: " + txtTenNhanVien.Text + " ?", "Chú ý", MessageBoxButtons.YesNo);
            if (r == System.Windows.Forms.DialogResult.Yes)
            {
                QuanLyNhanVien.UniqueObject.Delete(txtMaNhanVien.Text);
                dataGridView1.DataSource = QuanLyNhanVien.UniqueObject.GetDataTable;
                MessageBox.Show("OK");
            }
          
        }

        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = QuanLyNhanVien.UniqueObject.GetDataTable;
        }
       

        public NhanVien GetNhanVienFromView()
        {
            NhanVien nv = new NhanVien();
            nv.CMND = txtCMND.Text;
            nv.DiaChi = txtDiaChi.Text;
            nv.GioiTinh = txtGioiTinh.Text;
            nv.MaChucVu = txtChucVu.Text;
            nv.MaNhanVien = txtMaNhanVien.Text;
            nv.NgaySinh = txtNgaySinh.Value;
            nv.NgayVaoLam = txtNgayVaoLam.Value;
            nv.SDT = txtSDT.Text;
            nv.TenNhanVien = txtTenNhanVien.Text;
            nv.MatKhau = matkhau;
            return nv;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                var r = dataGridView1.Rows[e.RowIndex];
                txtChucVu.Text = r.Cells["ChucVu"].Value.ToString();
                txtDiaChi.Text = r.Cells["DiaChi"].Value.ToString();
                txtGioiTinh.Text = r.Cells["GioiTinh"].Value.ToString();
                txtMaNhanVien.Text = r.Cells["MaNhanVien"].Value.ToString();
                if (r.Cells["NgaySinh"].Value.ToString().Length > 2)
                    txtNgaySinh.Value = DateTime.Parse(r.Cells["NgaySinh"].Value.ToString());
                if (r.Cells["NgayVaoLam"].Value.ToString().Length > 2)
                    txtNgayVaoLam.Value = DateTime.Parse(r.Cells["NgayVaoLam"].Value.ToString());
                txtSDT.Text = r.Cells["SDT"].Value.ToString();
                txtTenNhanVien.Text = r.Cells["TenNhanVien"].Value.ToString();
                txtCMND.Text = r.Cells["CMND"].Value.ToString();
                matkhau = r.Cells["MatKhau"].Value.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            txtChucVu.Text = "";
            txtCMND.Text = "";
            txtDiaChi.Text = "";
            txtGioiTinh.Text = "";
            txtMaNhanVien.Text = "";
            txtSDT.Text = "";
            txtTenNhanVien.Text = "";
           
        }

        private void txtTimkiem_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = QuanLyNhanVien.UniqueObject.TimKiem(txtTimkiem.Text);
        }
    }
}
